namespace Warehouse

open FSharp.Windows

type ClientDetailsController(repository : DataModel.Repository) = 
    inherit Controller<ClientDetailsEvents, ClientDetailsModel>()

    override this.InitModel _ = ()

    override this.Dispatcher = Sync << function 
        | Save ok -> this.Save ok
        | SameAsBilling -> this.SameAsBilling

    member this.Save ok model = 
        this.ValidateBeforeSave model
        if model.IsValid then
            model.SaveEntity repository
            if model.Id = 0 then model.Id <- model.Entity.Id
            ok()

    member this.SameAsBilling model =
        model.ShipToAddress <- model.BillToAddress
        model.ShipToCity <- model.BillToCity
        model.ShipToState <- model.BillToState
        model.ShipToZipCode <- model.BillToZipCode

    member this.ValidateBeforeSave model =
        model |> Validation.textRequired  <@ fun m -> m.CompanyName @>
